package com.hsq.androidproject.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.example.lenovo.androidproject3.R;
import com.hsq.androidproject.entity.find.FindMineTL;
import com.hsq.androidproject.ui.ImageActivity;

import java.util.List;


/**
 * Created by cuilibao on 2017/3/6.
 */

public class GridViewImgsAdapter extends BaseAdapter {
    private Context context;
    private List<FindMineTL.TopicsBean.PhotoesBean> picUrlsList;

    public GridViewImgsAdapter(Context context, List<FindMineTL.TopicsBean.PhotoesBean> picUrlsList) {
        this.context = context;
        this.picUrlsList = picUrlsList;
    }

    @Override
    public int getCount() {
        return picUrlsList.size();
    }

    @Override
    public FindMineTL.TopicsBean.PhotoesBean getItem(int position) {
        return picUrlsList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_grid, parent, false);
            holder.imageView = (ImageView) convertView.findViewById(R.id.item_grid_iv);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        //得到父容器
        GridView gridView = (GridView) parent;
        //GridView的宽度
        int width = gridView.getWidth();
        //横向间距
        int horizontalSpacing = gridView.getHorizontalSpacing();
        //列数
        int numColumns = gridView.getNumColumns();
        int itemWidth = (width - horizontalSpacing * (numColumns - 1) - gridView.getPaddingLeft() - gridView.getPaddingRight()) / numColumns;
        //宽高参数
        RelativeLayout.LayoutParams params = new
                RelativeLayout.LayoutParams(itemWidth, itemWidth);
        holder.imageView.setLayoutParams(params);
        //计算每一个itemm的宽高
        //1:间距 列数
        //2:imageview
        //绑定数据
        final FindMineTL.TopicsBean.PhotoesBean picUrls = getItem(position);
        Glide.with(context).load(picUrls.getThumb()).into(holder.imageView);
        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ////////大图
                String bigImg = picUrls.getImage();
                Intent intent = new Intent(context, ImageActivity.class);
                intent.putExtra("bigImg", bigImg);
                context.startActivity(intent);
            }
        });
        return convertView;
    }

    class ViewHolder {

        ImageView imageView;
    }
}
